/**
 * @description 测试order数据
 */

const OrderModel = require("../../models/orderModel");
const addressModel = require("../../models/addressModel");
const shopItemModel = require("../../models/shopItemModel");

!(async () => {
  const requestBody = {
    addressId: "68e9b50231cf1dbbdceebe20",
    shopId: "68e9b53e92353877f77bc11d",
    shopName: "Sam",
    isCanceled: false,
    products: [
      {
        id: "68e9f6810f288e5c31bb4c33",
        num: 5,
      },
      {
        id: "68e9f6810f288e5c31bb4c35",
        num: 15,
      },
    ],
  };

  const address = await addressModel.findById(requestBody.addressId);
  const IdArray = requestBody.products.map((item) => item.id);
  const productArr = await shopItemModel.find({
    _id: {
      $in: IdArray,
    },
  });
  //   console.log(productArr);
  const prodectWithSales = productArr.map((item) => {
    const choosenItem = requestBody.products.filter(
      (p) => p.id === item._id.toString()
    );
    console.log(choosenItem);
    return { product: item, orderSales: choosenItem[0].num };
  });
  console.log(prodectWithSales);
  //   let productWithsales =  requestBody.products.map(async (item) => {
  //     const res = { product: {}, orderSales: item.num };
  //     const itemId = item.id;
  //     const shopItemRes = await shopItemModel.findOne({ _id: itemId });
  //     console.log(shopItemRes);
  //     res.product = shopItemRes;
  //     return res;
  //   });
  //   console.log(productWithsales);
  const result = await OrderModel.create({
    username: "zhangsan",
    shopName: requestBody.shopName,
    shopId: requestBody.shopId,
    isCanceled: true,
    address,
    products: prodectWithSales,
  });
  console.log(result);
})();
